/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import control.Local_cont;
import javax.swing.table.DefaultTableModel;
import model.Local;

/**
 *
 * @author Diego
 */
public class JInternalFrame_local_cont extends javax.swing.JInternalFrame {

    /**
     * Creates new form JInternalFrame_local_cont
     */
    public JInternalFrame_local_cont() {
        initComponents();
    }

    public void listar() {
        modelo = (DefaultTableModel) Tabela_local.getModel();
        modelo.setNumRows(0);
        local_cont = new Local_cont();
        for (int i = 0; i < local_cont.read().size(); i++) {
            modelo.addRow(new Object[]{local_cont.read().get(i).getIdlocal(),
                        local_cont.read().get(i).getNome(),
                        local_cont.read().get(i).getEndereco()});
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        Tabela_local = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        txt_local = new javax.swing.JTextField();
        btn_excluir = new javax.swing.JButton();
        btn_novo = new javax.swing.JButton();
        btn_sair = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);

        Tabela_local.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Id local", "Nome", "Endereço"
            }
        ));
        Tabela_local.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                Tabela_localMouseClicked(evt);
            }
        });
        Tabela_local.addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                Tabela_localAncestorAdded(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });
        Tabela_local.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                Tabela_localKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(Tabela_local);

        jLabel1.setText("Digite o nome do lacol para busca-lo");

        txt_local.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txt_localKeyReleased(evt);
            }
        });

        btn_excluir.setText("Excluir");
        btn_excluir.setEnabled(false);
        btn_excluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_excluirActionPerformed(evt);
            }
        });

        btn_novo.setText("Novo");
        btn_novo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_novoActionPerformed(evt);
            }
        });

        btn_sair.setText("Sair");
        btn_sair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_sairActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(txt_local, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 773, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btn_sair)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btn_novo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn_excluir)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(1, 1, 1)
                .addComponent(txt_local, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 295, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 68, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_excluir)
                    .addComponent(btn_novo)
                    .addComponent(btn_sair))
                .addContainerGap())
        );

        setBounds(0, 0, 809, 479);
    }// </editor-fold>//GEN-END:initComponents

    private void btn_novoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_novoActionPerformed
        modelo = (DefaultTableModel) Tabela_local.getModel();
        modelo.addRow(new Object[WIDTH]);
    }//GEN-LAST:event_btn_novoActionPerformed

    private void btn_sairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_sairActionPerformed
        dispose();
    }//GEN-LAST:event_btn_sairActionPerformed

    private void txt_localKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_localKeyReleased
        modelo = (DefaultTableModel) Tabela_local.getModel();
        modelo.setNumRows(0);
        local_cont = new Local_cont();
        local = new Local();
        local.setNome(txt_local.getText());
        if (txt_local.getText().equals("")) {
            listar();
            btn_excluir.setEnabled(false);
        } else {
        for (int i = 0; i < local_cont.search(local).size(); i++) {
            modelo.addRow(new Object[]{local_cont.search(local).get(i).getIdlocal(),
                        local_cont.search(local).get(i).getNome(),
                        local_cont.search(local).get(i).getEndereco()});
            }
        }
    }//GEN-LAST:event_txt_localKeyReleased

    private void Tabela_localMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_Tabela_localMouseClicked
        btn_excluir.setEnabled(true);
    }//GEN-LAST:event_Tabela_localMouseClicked

    private void btn_excluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_excluirActionPerformed
        local_cont = new Local_cont();
        local = new Local();
        modelo = (DefaultTableModel) Tabela_local.getModel();
        if (Tabela_local.getValueAt(Tabela_local.getSelectedRow(), 0) == null) {
            modelo.removeRow(Tabela_local.getSelectedRow());
        } else {
            local.setIdlocal(Integer.valueOf(Tabela_local.getValueAt(Tabela_local.getSelectedRow(), 0).toString()));
            local_cont.delete(local);
            listar();
        }
        txt_local.setText(null);
        btn_excluir.setEnabled(false);
    }//GEN-LAST:event_btn_excluirActionPerformed

    private void Tabela_localAncestorAdded(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_Tabela_localAncestorAdded
        listar();
    }//GEN-LAST:event_Tabela_localAncestorAdded

    private void Tabela_localKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_Tabela_localKeyReleased
        local_cont = new Local_cont();
        local = new Local();
        if (Tabela_local.getValueAt(Tabela_local.getSelectedRow(), 0) == null) {
            local.setNome(Tabela_local.getValueAt(Tabela_local.getSelectedRow(), 1).toString());
            local.setEndereco(Tabela_local.getValueAt(Tabela_local.getSelectedRow(), 2).toString());
            local_cont.create(local);
        } else {
            local.setIdlocal(Integer.valueOf(Tabela_local.getValueAt(Tabela_local.getSelectedRow(), 0).toString()));
            local.setNome(Tabela_local.getValueAt(Tabela_local.getSelectedRow(), 1).toString());
            local.setEndereco(Tabela_local.getValueAt(Tabela_local.getSelectedRow(), 2).toString());
            local_cont.update(local);
        }
        listar();
        btn_excluir.setEnabled(false);
    }//GEN-LAST:event_Tabela_localKeyReleased
    private DefaultTableModel modelo;
    private Local local;
    private Local_cont local_cont;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable Tabela_local;
    private javax.swing.JButton btn_excluir;
    private javax.swing.JButton btn_novo;
    private javax.swing.JButton btn_sair;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField txt_local;
    // End of variables declaration//GEN-END:variables
}
